





//起始时间 : 1980年一月一日
//前面有一个空位
//一年有 闰年 和 平年 分别 366天 和 365天
//算出今年是闰年还是平年
var year = new Date().getFullYear();  //当前年份
var mounth = new Date().getMonth();  //当前月份从0开始所以要加一
var mounth =  ++mounth;
//总天数
var days = 0;



//头部
document.querySelector("#nian").innerHTML = year;
document.querySelector("#yue").innerHTML = mounth;







//判断这一年是不是闰年，如果是返回true；如果不是返回false；
/**
 * @return {boolean}
 */
function RunNian(l){
    if (l % 400 === 0 || (l % 4 === 0 && l % 100 !==0)){
        return true;
    }else{
        return false;
    }
}

//算出1900年到现在的上一年的总天数
for(var i = 1980; i < year; i++){
    if(RunNian(i)){
        days += 366;
    }else{
        days += 365;
    }
}
console.log(days);


//算出今年的一月份到这个月的上一个月有多少天
for(var k = 1; k<mounth; k++){
    switch(k){
        case 1:
            days += 31;
            break;
        case 2:
            if (RunNian(year)){
                days += 29;
            }else{
                days += 28;
            }
            break;
        case 3:
            days += 31;
            break;
        case 4:
            days += 30;
            break;
        case 5:
            days += 31;
            break;
        case 6:
            days += 30;
            break;
        case 7:
            days += 31;
            break;
        case 8:
            days += 31;
            break;
        case 9:
            days += 30;
            break;
        case 10:
            days += 31;
            break;
        case 11:
            days += 30;
            break;
        case 12:
            days += 31;
            break;
    }
}



//算一下本月有多少天
var ThisMonth = 0;
switch(mounth){
    case 1:
        ThisMonth += 31;
        break;
    case 2:
        if (RunNian(year)){
            ThisMonth += 29;
        }else{
            ThisMonth += 28;
        }
        break;
    case 3:
        ThisMonth += 31;
        break;
    case 4:
        ThisMonth += 30;
        break;
    case 5:
        ThisMonth += 31;
        break;
    case 6:
        ThisMonth += 30;
        break;
    case 7:
        ThisMonth += 31;
        break;
    case 8:
        ThisMonth += 31;
        break;
    case 9:
        ThisMonth += 30;
        break;
    case 10:
        ThisMonth += 31;
        break;
    case 11:
        ThisMonth += 30;
        break;
    case 12:
        ThisMonth += 31;
        break;
}
console.log(ThisMonth);



//算本月的头一个月有多少天
var LastMonth = 0;
if (mounth == 1){
    LastMonth = 31;
}else{
    switch(mounth - 1){
        case 1:
            LastMonth += 31;
            break;
        case 2:
            if (RunNian(year)){
                LastMonth += 29;
            }else{
                LastMonth += 28;
            }
            break;
        case 3:
            LastMonth += 31;
            break;
        case 4:
            LastMonth += 30;
            break;
        case 5:
            LastMonth += 31;
            break;
        case 6:
            LastMonth += 30;
            break;
        case 7:
            LastMonth += 31;
            break;
        case 8:
            LastMonth += 31;
            break;
        case 9:
            LastMonth += 30;
            break;
        case 10:
            LastMonth += 31;
            break;
        case 11:
            LastMonth += 30;
            break;
        case 12:
            LastMonth += 31;
            break;
    }
}

console.log(LastMonth);


//因为1980年一月一日是从星期二开始，我们要让它从星期日开始所以
days += 2;
console.log(days);

//算出本月一号是星期几起始位置
var start = days % 7;

//算出当月日历多少天
var num = 42;  //6*7=42

//算出下个月有多少天
var nextMounth = num - start - ThisMonth;
console.log(nextMounth);


//算当前日期第几号
var thisDays = new Date().getDate();
console.log(thisDays);




document.querySelector("#date1");
//第一个for循环
for(var j = LastMonth - start +1; j<=LastMonth; j++){
    date1.innerHTML += "<span class='h'>" + j + "</span>";
}

//第二个for循环
for(var k = 1; k<=ThisMonth; k++){
    if (k == thisDays){
        date1.innerHTML += "<span class='today'>" + k + "</span>"
    }else{
        date1.innerHTML += "<span>" + k + "</span>"
    }
}

//第三个for循环
for(var s = 1; s<=nextMounth; s++){
    date1.innerHTML += "<span class='h'>" + s + "</span>"
}